package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.AnswerType;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.AnswerTypeService;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import cn.com.connext.scrm.web.rest.req.*;
import cn.com.connext.scrm.web.rest.resp.QueryAnswerTitleResp;
import cn.com.connext.scrm.web.rest.resp.QueryAnswerTypeResp;
import com.alibaba.fastjson.JSON;
import com.connext.common.exception.ServiceException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 问答分类相关接口
 */
@RestController
@RequestMapping("/api/answer")
public class AnswerTypeResource {
    private final Logger log = LoggerFactory.getLogger(AnswerTypeResource.class);

    @Autowired
    private AnswerTypeService answerTypeService;

    @PostMapping("/type-save")
    @ApiOperation(value = "创建问答分类", tags = {"问答列表"}, notes = "创建问答分类")
    public ResponseEntity<Boolean> saveAnswerType(@RequestBody AnswerTypeReq req) {
        log.info("创建问答分类,请求参数req : {}", JSON.toJSONString(req));
        if (req == null) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        AnswerType answerType = new AnswerType();
        answerType.setCreateTime(new Date());
        answerType.setUpdateTime(new Date());
        answerType.setIsDelete(0);//未删除
        BeanUtils.copyProperties(req, answerType);
        log.info("创建问答分类,answerType : {}", JSON.toJSONString(answerType));
        return answerTypeService.saveAnswerType(answerType);
    }

    @PostMapping("/type-update")
    @ApiOperation(value = "修改问答分类", tags = {"问答列表"}, notes = "创建问答分类")
    public ResponseEntity<Boolean> updateAnswerType(@RequestBody AnswerTypeUpdateReq req) {
        log.info("创建问答分类,请求参数req : {}", JSON.toJSONString(req));
        if (StringUtils.isBlank(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return answerTypeService.updateAnswerType(req);
    }

    /**
     * 问答分类查询，根据标题和内容模糊查询
     */
    @PostMapping("/type-query")
    @ApiOperation(value = "查询问答分类", tags = {"问答列表"}, notes = "查询问答分类")
    public ResponseEntity<Page<QueryAnswerTypeResp>> queryAnswerType(@RequestBody AnswerTypeQueryReq req) {
        log.info("查询问答分类,请求参数req : {}", JSON.toJSONString(req));
        Page answerTypes = answerTypeService.answerTypeList(req);
        return ResponseEntity.ok(answerTypes);
    }

    @PostMapping("/type-delete")
    @ApiOperation(value = "根据主键删除问答分类", tags = {"问答列表"}, notes = "根据主键删除问答分类")
    public ResponseEntity<Boolean> deleteAnswerType(@RequestBody DeleteReq req) {
        log.info("根据主键删除问答分类,请求参数req: {}", JSON.toJSONString(req));
        if (StringUtils.isEmpty(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return answerTypeService.deleteAnswerType(req.getId());
    }

    /**
     * 获取问答分类标题枚举
     */
    @PostMapping("/type-query-title")
    @ApiOperation(value = "获取问答分类标题枚举", tags = {"问答列表"}, notes = "获取问答分类标题枚举")
    public ResponseEntity<List<QueryAnswerTitleResp>> queryAnswerTypeTitle() {
        log.info("获取问答分类标题枚举");
        return answerTypeService.queryAnswerTypeTitle();
    }

    /**
     * 问答浏览接口 /api/browserecord/create
     * 问答种类点赞
     */
    @PostMapping("/likeComment")
    @ApiOperation(value = "评论点赞", tags = {"评论列表"}, notes = "评论点赞")
    public JsonResult likeComment(@RequestBody AnswerLikeCommentReq req) {
        if (StringUtils.isEmpty(req.getId()) || StringUtils.isEmpty(req.getUserId())) {
            log.info("评论点赞,请求参数req: {}", JSON.toJSONString(req));
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return answerTypeService.likeComment(req);
    }
}
